package com.mars.admin.modules.activity.service.impl;

import com.mars.admin.common.request.activity.ActivityCategoryRequest;
import com.mars.admin.framework.exception.BusinessException;
import com.mars.admin.modules.activity.entity.ActivityCategory;
import com.mars.admin.modules.activity.service.IActivityCategoryService;
import com.mars.admin.modules.base.service.impl.BaseServiceImpl;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import static com.mars.admin.modules.activity.entity.table.ActivityCategoryTableDef.ACTIVITY_CATEGORY;

/**
 * 代码领取微信公众号【程序员Mars】
 *
 * @className: ActivityCategoryImpl
 * @author: Mars
 * @date: 2025/7/28 22:52
 */
@Service
public class ActivityCategoryImpl extends BaseServiceImpl<ActivityCategory> implements IActivityCategoryService {

    @Override
    public void add(ActivityCategoryRequest request) {
        ActivityCategory activityCategory = new ActivityCategory();
        String name = request.getName();
        if (StringUtils.isEmpty(name)) {
            throw new BusinessException("活动名称不能为空");
        }
        // 检查名称是否已存在，并排除已删除的记录
        if (this.count(QueryWrapper.create()
                .eq(ActivityCategory::getName, name)
                .eq(ActivityCategory::getIsDeleted, 0)) > 0) {
            throw new BusinessException("活动名称已存在");
        }
        BeanUtils.copyProperties(request, activityCategory);
        this.save(activityCategory);
    }

    @Override
    public Page<ActivityCategory> page(ActivityCategoryRequest request) {
        Page<ActivityCategory> page = new Page<>(request.getPageNumber(), request.getPageSize());
        QueryWrapper query = QueryWrapper.create()
                .select(ACTIVITY_CATEGORY.ALL_COLUMNS)
                .from(ACTIVITY_CATEGORY)
                .where(ACTIVITY_CATEGORY.IS_DELETED.eq(0))
                .orderBy(ActivityCategory::getCreateTime, false);

        // 分类名称模糊搜索
        if (StringUtils.isNotEmpty(request.getName())) {
            query.and(ACTIVITY_CATEGORY.NAME.like(request.getName()));
        }

        // 状态条件
        if (request.getStatus() != null) {
            query.and(ACTIVITY_CATEGORY.STATUS.eq(request.getStatus()));
        }

        // 是否热门条件
        if (request.getIsHot() != null) {
            query.and(ACTIVITY_CATEGORY.IS_HOT.eq(request.getIsHot()));
        }

        return this.page(page, query);
    }

    @Override
    public void updateCategory(ActivityCategoryRequest request) {
        ActivityCategory activityCategory = this.getById(request.getId());
        if (activityCategory == null) {
            throw new BusinessException("分类不存在");
        }
        BeanUtils.copyProperties(request, activityCategory);
        this.updateById(activityCategory);
    }

    @Override
    public void delete(Long id) {
        ActivityCategory activityCategory = this.getById(id);
        if (activityCategory == null) {
            throw new BusinessException("分类不存在");
        }
        this.removeById(id);
    }

    @Override
    public ActivityCategory getById(Long id) {
        return this.getMapper().selectOneById(id);
    }
}
